package com.leyuangame.core.controller.api;

import com.leyuangame.core.BMDataContext;
import com.leyuangame.core.config.SysConfig;
import com.leyuangame.core.controller.bean.ResponseHandler.LeYuanResult;
import com.leyuangame.core.model.GameCategory;
import com.leyuangame.core.model.RebotConfig;
import com.leyuangame.dao.repository.jpa.GameCategoryRepository;
import com.leyuangame.dao.repository.jpa.PlayUserLogRepository;
import com.leyuangame.dao.repository.jpa.PlayUserRepository;
import com.leyuangame.dao.repository.jpa.RoleAuthRepository;
import com.leyuangame.redis.JedisClient;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigDecimal;
import java.util.List;

@Controller
public class TestController {
    @Autowired
    private BMDataContext bmDataContext;

    @Autowired
    private PlayUserRepository playUserRepository;

    @Autowired
    private PlayUserLogRepository playUserLogRepository;
    @PersistenceContext
    private EntityManager entityManager;
    @Autowired
    private JedisClient jedisClient;
    @Autowired
    RoleAuthRepository roleAuthRepository;
    @Autowired
    private GameCategoryRepository gameCategoryRepository;
    @RequestMapping(value = "/test",produces = "application/json")
    @ResponseBody
    public LeYuanResult test(String id){
        //String id = "103d1852766b44939a4b7b25c075113f";
//        Query query = entityManager.createNativeQuery("select * " +
//                "from bm_playuser a " +
//                "left join user_log b " +
//                "on a.id = b.user_id " +
//                "where a.id = ?");
//        List list = query.unwrap(SQLQuery.class).setParameter(0,id).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
//        int rows = query.getFirstResult();
//        System.out.println("123S");
//        Long testkey = jedisClient.llen("testkey");
//        List<String> result = jedisClient.lrange("testkey", (long) 0, testkey);
//        List<GameCategory> res = gameCategoryRepository.findGameCategoriesByIsopenOrderBySort(0);
        RebotConfig config = new RebotConfig();
        config.setIsopen(0);
        config.setMingold(new BigDecimal(20));
        config.setMaxgold(new BigDecimal(300));
        SysConfig.saveRebot(config);
        return LeYuanResult.ok(SysConfig.rebotConfig);
    }
}
